#include <pthread.h>
#include <cstdio>

#define NUM 5

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  cond = PTHREAD_COND_INITIALIZER;
int ticket = 100;

void* get_ticket(void *args)
{
    pthread_detach(pthread_self());
    while(1)
    {
        pthread_mutex_lock(&lock);

        if(ticket > 0)ticket--;
        else
        {
            pthread_mutex_unlock(&lock);
            break;
        }
        printf("%d :snap up ticket\n",pthread_self());
        pthread_mutex_unlock(&lock);
    }
}

int main()
{
    for(int i = 0;i < NUM; i++)
    {
        pthread_t pid;
        pthread_create(&pid,nullptr,get_ticket,nullptr);
    }
    pthread_mutex_destroy(&lock);
    return 0;
}